<?php
/**
 * Update Customer
 */

use Nawa\Messages;
use Nawa\Customer;
use Fox\Generator;
use Fox\Html\Button;
use Fox\Common\Customer\Partner;
use Fox\SalesDistribution\Sales\Partner\Role;
use Fox\Html\TabPanel\Tab,Fox\Html\Wrapper,Fox\Html\TabPanel,Fox\Html\Screen;

$id = Xview::instance()->val("customerid");
try {
	$customer = new Customer($id);
	$data = $customer->getData();

}
catch(Fox\Object\Exception\NotExist $e)
{
	Xview::instance()->systemError();
}
if(!$customer->exists())
{
	Xview::instance()->systemError();
}
$screen = new Screen("eCustomer","update");
$crumbs = Xview::instance()->component('medicalstudies','breadcrumb',
array('deepness' => 0,
						'customerid' => $id,
						'customer_name' => $customer->name1,
						'msCounter' => $customer->medicalStudiesCount()));
$screen->breadcrumb()->crumbs($crumbs);

$screen->toolbar()->add(new Button("add","?mod=medicalstudies&act=create&sourceid=$id",Msg(Messages::BUNDLE,Messages::LABEL_MS_CREATE_NEW)));

$screen->add(new Button("idele",Generator::display("delete","?mod=customers&act=delete&customerid=$id")))
->color(Button::COLOR_RED);
$tabPanel = new TabPanel("pCustomer");
$wMasterData = new Wrapper("wMasterData");
$blName = Xview::instance()->component("customers", "name");
$wMasterData->add($blName);
$blAddress = Xview::instance()->component("customers", "address");
$wMasterData->add($blAddress);
$tabPanel->add(new Tab("tMasterData",Msg(Fox\Msg::BUNDLE,Fox\Msg::TITLE_MASTER_DATA),$wMasterData));
$blPartners = Xview::instance()->component("customers", "partners");
//$tabPanel->add(new Tab("tPartners",Msg(Customer::BUNDLE_NAME,40),$blPartners));
//$blParents = Xview::instance()->component("customers", "attached");
if(isset($data['gParents']) && count($data['gParents']) > 0)
{
	$tabPanel->add(new Tab("tParents",Msg(Customer::BUNDLE_NAME,43),$blParents));
}
$screen->add($tabPanel);
$screen->initialize($data);

if($screen->submitted())
{
	$inputs = $screen->get();
	//
	if(!$screen->errors())
	{
		try {
			// Create Customer
			$customer->setFields($inputs);
			$customer->prepareUpdate(Customer::CHANGE_DEFAULT);
			$customer->update();
			Fox\Message::Information(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_UPDATE,"<b>".$customer->name1."</b>"));
			Xview::instance()->redirect("?mod=customers&act=update&customerid=$id");
		}
		catch(Fox\Object\Exception\Save $e)
		{
			Fox\Message::Error($e->getMessage());
		}
		catch(Exception $e)
		{
			Fox\Message::Error($e->getMessage());
		}
	}
}
$screen->mainTitle("<div class=\"page-title\">".Msg(Messages::BUNDLE,Messages::LABEL_CUSTOMER_UPDATE)."</div>");
echo $screen->Display();